home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: [Q] Experts: Multi-dim dynamic arrays
- Date: Tue, 02 Apr 96 11:29:11 GMT
- Organization: none
- Distribution: usa
- Message-ID: <828444551snz@genesis.demon.co.uk>
- References: <4jqc3d$lru@peabody.colorado.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jqc3d$lru@peabody.colorado.edu>
- rangaswa@eddie.Colorado.EDU "qwertyuiop" writes:
-
- >Here is a synopsis of the code I tried: I get no compilation errors, but
- >during execution, I get floating point exception at the line marked XX.
- >(Necessary file pointers, header files etc. are included but not shown here!.)
- >
- >int **imatrix(int nrl,int nrh,int ncl,int nch);
- >main()
- >{
- >
- >int i, j; /* loop variables */
- >int R, C; /* number of rows, columns */
- >int **Table; /* table of data */
- >int X; /* scratch variable */
- >
- >fscanf(fileptr, "%d %d\n", &R, &C);
- >Table = imatrix(1,R,1,C);
-
- You are attempting (whether successfully or not) to create an effective 2D
- array indexed from 1 to R, 1 to C.
-
- >for(i = 0; i < R; i++)
- > {
- > for(j = 0; j < C; j++)
-
- And now you try to use it indexed from 0 to R-1, 0 to C-1. Clearly you
- are going out of bounds. Allocate with:
-
- Table = imatrix(0,R-1,0,C-1);
-
- > {
- > fscanf(fileptr, "%d", &X); <------There is no need for X here, still!
- > *(*(A+i)+j) = X; XX<------floating point exception occurs here!
-
- More commonly and clearly written as:
-
- A[i][j] = X;
-
- You haven't declared A here, did you mean Table?
-
- > }
- > fscanf(fileptr, "\n");
-
- While it probably works here remember that "\n" matches while-space in general,
- it doesn't simply read until the end of the line. You don't need it before %d
- since that skips leading white-space.
-
- > }
- >....
- >}
- >
- >int **imatrix(int nrl, int nrh, int ncl, int nch) /* see numerical recipes! */
- >{
- >int i, **m;
- >m = (int **)malloc((unsigned)(nrh-nrl+1)*sizeof(int*));
- >if(!m) nerror("alloc failure, exiting!..\n");
- >m -= nrl;
- >
- >for(i=nrl;i<=nrh;i++)
- > {
- > m[i] = (int*)malloc((unsigned)(nch-ncl+1)*sizeof(int));
- > if(!m[i]) nerror("alloc failure etc...\n");
- > m[i] -= ncl;
- > }
- >return m;
- >}
-
- This is a classic example of illegal code and a well known problem with
- Numerical Recipies. The problem is that m -= nrl and m[i] -= ncl typically
- generate pointers pointing outside the array which results in undefined
- behaviour. It is OK in this case since you should be passing nrl and ncl as
- 0.
-
- >----------------------------------------------------------------------------
- >Questions: 1. On my machine, size of int, int * are both 4 bytes. I'm using
- >a gnu compiler on a Unix machine. Why do I get a floating point exception
- >at the line mentioned above?
-
- You invoked undefined behaviour - anything can happen.
-
- >2. The imatrix function returns a pointer to an array of pointers to rows.
- >I ran a quick address and content check to see if my dereferencing of pointers
- >is faulty. There is a difference of 32 bytes between the address of Table[i]
- >and Table[i+1], how come?
-
- Do you mean a difference in the values?
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-